package com.okbihuo.perm.modules.common.rest;

import com.okbihuo.perm.core.tool.api.R;
import com.okbihuo.perm.modules.rwb.qw.domain.QwCorpAuth;
import com.okbihuo.perm.modules.rwb.qw.service.QwCorpAuthService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.RandomUtils;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;


@RestController
@RequestMapping("/open/common/corp")
@RequiredArgsConstructor
@Api(value = "企业设置", tags = "企业设置接口")
public class OpenCorpController {

    private final QwCorpAuthService corpAuthService;

    @RequestMapping("getJsConfig")
    public Object getJsConfig(@RequestParam("url") String url) {
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId("ww03bca02e077c30dc");
        config.setCorpSecret("80A35iWniE2dOtWkVt6sFU3WQznAA-VuIuj8Wg8bfGs");
        final WxCpService wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
        try {
            final WxJsapiSignature signature = createAgentJsapiSignature(wxCpService,URLDecoder.decode(url, "UTF-8"));
            return R.data(signature);
        } catch (WxErrorException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return R.fail("fail");
    }

    @RequestMapping("getJsConfigByCorpId")
    public Object getJsConfigByCorpId(@RequestParam("corpId") String corpId,@RequestParam("url") String url) {
        final QwCorpAuth corpAuth = corpAuthService.getByCorpId(corpId);
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId(corpAuth.getCorpId());
        config.setCorpSecret(corpAuth.getAgentSecret());
        final WxCpService wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
        try {
            final WxJsapiSignature signature = createAgentJsapiSignature(wxCpService,URLDecoder.decode(url, "UTF-8"));
            return R.data(signature);
        } catch (WxErrorException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return R.fail("fail");
    }
    @RequestMapping("getAgentJsapiTicket")
    public Object getAgentJsapiTicket(@RequestParam("corpId") String corpId) throws WxErrorException {
        final QwCorpAuth corpAuth = corpAuthService.getByCorpId(corpId);
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId(corpAuth.getCorpId());
        config.setCorpSecret(corpAuth.getAgentSecret());
        final WxCpService wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
        return R.data( wxCpService.getAgentJsapiTicket());
    }
    private WxJsapiSignature createAgentJsapiSignature(WxCpService wxCpService, String url) throws WxErrorException {
        long timestamp = System.currentTimeMillis() / 1000;
        String noncestr = RandomUtils.getRandomStr();
        String jsapiTicket = wxCpService.getAgentJsapiTicket();
        String signature = SHA1.genWithAmple(
                "jsapi_ticket=" + jsapiTicket,
                "noncestr=" + noncestr,
                "timestamp=" + timestamp,
                "url=" + url
        );
        WxJsapiSignature jsapiSignature = new WxJsapiSignature();
        jsapiSignature.setTimestamp(timestamp);
        jsapiSignature.setNonceStr(noncestr);
        jsapiSignature.setUrl(url);
        jsapiSignature.setSignature(signature);

        // Fixed bug
        jsapiSignature.setAppId(wxCpService.getWxCpConfigStorage().getCorpId());

        return jsapiSignature;
    }
}
